<?php

/**
 * Created by PhpStorm.
 * User: kc
 * Date: 2017/2/24
 * Time: 14:36
 */
class StoreController extends BaseController{
    /**
     * @Author:      kc
     * @DateTime:    2017-02-24 14:42:29
     * @Description: 门店列表
     */
    public function storeList(){
        $validator = Validator::make(Input::all(),
            array(
                'city' =>'required',
            )
        );
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $city = ResourceCityType::getCity(Input::get('city'));
            if(!empty($city)){
                $store = MaintenanceShops::getShop($city->id);
                $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
                foreach($store as $v){
                    $order_count = Orders::getToMaintenanceShops(isset($wechat_user->id)?$wechat_user->id:Null,$v->id);
                    if($order_count != 0){
                        $v->to_shop = 1;
                    }else{
                        $v->to_shop = 0;
                    }
                }
                foreach($store as $key=>$s){
                    $store[$key]['shop_url']=Config::get('config.shop_img_url');
                }
            }else{
                $store = "";
            }

            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $store
            );
        }while(false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "store_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:   2017-02-27 11:42:29
     * @Description: 门店详情
     */
    public function storeDetails(){
        $validator = Validator::make(Input::all(),
            array(
                'shop_id' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get("error.args_error_10000");
                $response['result'] = $validator->messages()->all();
                break;
            }
            $maintenance_shops = MaintenanceShops::getDetails(Input::get('shop_id'));
            if(!is_null($maintenance_shops)) {
//                $order_id = Orders::where('shop_id',$maintenance_shops->id)
//                    ->select('order_id')
//                    ->where('status',1018)
//                    ->get()
//                    ->toArray();
//                $Evaluation = OrderEvaluation::whereIn('order_id',$order_id)->where('is_show',1)->avg('star');
                $Evaluation = Orders::leftjoin('order_evaluation as oe','orders.order_id','=','oe.order_id')->where('orders.shop_id',$maintenance_shops->id)
                    ->where('oe.is_show',1)->where('orders.status',1018)->avg('oe.star');
                $star = ceil($Evaluation);
                if((int)$star === 0){
                    $maintenance_shops->star = 5;
                }else if((int)$star < 3){
                    $maintenance_shops->star = 3;
                }else{
                    $maintenance_shops->star = $star;
                }
                $wechat_user=$this->verificationUserToken(Input::get("token"),$response);
                $order_count = Orders::getToMaintenanceShops(isset($wechat_user->id)?$wechat_user->id:Null,Input::get('shop_id'));
                if($order_count != 0){
                    $maintenance_shops->to_shop = 1;
                }else{
                    $maintenance_shops->to_shop = 0;
                }
                $maintenance_shops->small_image=Config::get('config.shop_img_url').$maintenance_shops->image; //暂时修改小图为大图显示
            }
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $maintenance_shops,
            );
        }while(false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "storeDetails", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}